package com.yeyuan.controll;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.yeyuan.entity.User;
import com.yeyuan.service.IUserService;
import com.yeyuan.service.impl.UserServiceImpl;
import com.yeyuan.util.listener.LoginServlet;
@Controller
@SessionAttributes("userName")
@RequestMapping("/user")
public class UserControll {
	@Resource
	IUserService userServiceImpl;
	

	
	
	@RequestMapping("/registerUser")  
    public ModelAndView registerUser(User user) {  
		ModelAndView model = new ModelAndView();
		user = userServiceImpl.registerUser(user);
		if(user.getUserID()!=0){
			model.addObject("userName", user.getUserName());
			model.setViewName("registerSuccess");
		}else{
			model.setViewName("login");
		}
		return model;
    }  
	
	
	@RequestMapping("/loginPross")  
	public ModelAndView loginPross(User user,ModelMap model1,HttpSession session,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { 
		ModelAndView model = new ModelAndView();
		LoginServlet loginServlet=new LoginServlet();
		if(userServiceImpl.loginUser(user)!=null){
			loginServlet.service(request, response);
			model1.addAttribute("userName",user.getUserName());
			model1.addAttribute("seesionID", session.getId());
			model.addObject("userName", user.getUserName());
			model.setViewName("main");
		}else{
			model.addObject("error", "x用户名或密码错误");
			model.setViewName("login");
		}
		return model;
	}
	

	@RequestMapping("/determine")
	@ResponseBody  
	public Map<String, String> determine(String userName){
		Map<String, String> map = new HashMap<String, String>(1);  
		if(userServiceImpl.getUserByUserName(userName)!=null){
			map.put("error","error");
		}else{
			map.put("success","true");
		}
		return map;
	}
	
	
	@RequestMapping("/testUser")
	@ResponseBody  
	public Map<String, String> testUser(HttpSession session){
		Map<String, String> map = new HashMap<String, String>(1);  
		if(session.getAttribute("userMsg")!=null){
			map.put("success",session.getAttribute("userMsg").toString());
		}else{
			map.put("success","");
		}
		return map;
	}
	
	@RequestMapping("/main")
	@ResponseBody  
	public ModelAndView searchAll(){
		ModelAndView model = new ModelAndView();
        model.setViewName("main");
        return model;
	}
	
	
	
	
	
	@RequestMapping("/login")
	public ModelAndView loginPage(){
		ModelAndView model = new ModelAndView();
		model.setViewName("login");
		return model;
	}
}
